define(function(require,exports,module){

	//检测类
	var Auth = require('qd-auth-1.0');
	var moneyRecharge = 0;
	var  userSafePayTip = {};
	userSafePayTip.msg=$("#binding_bank_message").val();
	userSafePayTip.status=$("#binding_bank_status").val();
	//判断是否已绑定银行卡
	
	if(userSafePayTip.status=='no_bank'){
		userSafePayTip.url="/member/cash/addBankPage.html";
	}else if(userSafePayTip.status=='no_phone'){
		userSafePayTip.url="/member/cash/bank.html";
	}
	userSafePayTip.msg=$("#binding_bank_message").val();
	/*$.ajax({
		url:"/member/bank/checkBinding.html",
		type:"get",
		dataType:"json",
		success:function(data){
			if(data.result==false){
				//$(".choosePayWay li").eq(1).trigger("click");
				userSafePayBankFlag = false;
				userSafePayTip = data;
				
				if(data.error_type=="no_bank"){
					userSafePayTip.url = "/member/cash/addBankPage.html";
				}else{
					userSafePayTip.url = "/member/cash/bank.html";
					window.layer.open({
						type: 1,
						closeBtn: 0,
						title: "&nbsp;",
						area: ['450px', '215px'],
						border: [1, 1, '#cecfd0'],
						content: '<div class="tipsWrap w450"><div class="tipsTxt"><i class="iconfont tipIco">&#xe63c;</i><span>'+data.msg+'</span></div><div class="tipsBtnBar"><a href="'+url+'" class="okBtn">立即绑定</a></div></div>'
					});				
				}
			}
		}
	});*/
	
	
	var wait = 60;
	var returnflag = false;
	get_code_time = function (elem) {
		if (wait == 0||returnflag==true) {
			$(elem).val("重新发送");
			$(elem).removeClass("addCardVaildCodeDis");
			$(elem).attr("disabled",false);
			wait = 60;
		} else {
			$(elem).val("("+wait+")秒后重新获取");
			$(elem).addClass("addCardVaildCodeDis");
			$(elem).attr("disabled",true);
			wait--;
			setTimeout(function() {
				get_code_time(elem);
			}, 1000);
		}
	};
	$("#getPhoneVcode").click(function(){
		validateMoney();
		if(flag==false) return;
		var _that = this;
		returnflag = false;
		get_code_time(_that);
		moneyRecharge = $("input[name='money']").val();
		
		$.ajax({
			url: "/member/pay/doQuicklyPay.html",
			type:"get",
			dataType:"json",
			data:{
				money: $("input[name='money']").val()
			}, 
			success: function(data) {
				if(data.result==true){
					$("#gettaken").val(data.ticket);
					$("#gettakens").val(data.tickets);
					$("#getorderId").val(data.orderId);
					//moneyRecharge = $("input[name='money']").val();
				}else{
					returnflag = true;
					$(".vaild-error").html(data.msg);
				}
			}
		});
    });
	
	//判断是否已认证或开通第三方接口
	$.ajax({
		url:"/member/recharge/checkNewRecharge.html?randomTime=" + (new Date()).getTime(),
		type:"get",
		dataType:"json",
		success:function(data){
			var urlt="/member/security/setting.html";
			if(data.msg=="请先升级密码,目前密码不安全！"){
				urlt="/member/security/setting.html?m=modifyPwd";
			 }
			if(!data.result){
				//构造确认框DOM
				window.layer.open({
					type: 1,
					closeBtn: 0,
	                title: "&nbsp;",
					area: ['450px', '190px'],
					border: [1, 1, '#cecfd0'],
					content: '<div class="tipsWrap w450"><div class="tipsTxt"><i class="iconfont tipIco">&#xe63c;</i><span>'+data.msg+'</span></div><div class="tipsBtnBar"><a href="'+urlt+'" class="okBtn">确定</a></div></div>'
				});
			}
		}
	})

	//TAB切换
	require.async('common1',function(){
		$(".user_right_border").tabChange({
			isClick:true,
            isHover:false,
			childLi:".recharge_list li",//tab选项卡
			childContent:".recharge_content",//tab内容
			hoverClassName:"active",//选中当前选项卡的样式
			callBack:false
		});
	});

	$(".recharge_list  li").on("click",function(){
		var statusVal =$(this).find("span").attr("data-value");
		if(statusVal==3)//线下充值
		{	
			$('.paymentStatus').val(2);
			if($("input[name='pay']").attr('disabled')=='disabled'){
				$("input[name='pay']").attr("disabled",false);
			}
			$("#J_reCharge").attr("action","/member/recharge/doOfflineRecharge.html");
			$("#J_reCharge").removeAttr("onsubmit");
			var newCashBtn = '<input type="button" name="name" class="cashBtn" value="确认提交" onClick="return reCharge();"/>';
			$(".paymentStatus").prev().remove();
			$(".paymentStatus").before(newCashBtn);
			
		}
		else if(statusVal==2){
			if($("input[name='pay']").attr('disabled')=='disabled'){
				$("input[name='pay']").attr("disabled",false);
			}
			$('.paymentStatus').val(1);
			$("#J_reCharge").attr("action","/member/recharge/doRecharge.html");
			$("#J_reCharge").attr("onsubmit","return reCharge();");
			var newCashBtn1 = '<input type="submit" name="name" class="cashBtn" value="确认提交"/>';
			$(".paymentStatus").prev().remove();
			$(".paymentStatus").before(newCashBtn1);
		}//线上充值
		else{
			$('.paymentStatus').val(1);
			$("input[name='pay']").attr('disabled','disabled');
			$("#J_reCharge").attr("action","/member/recharge/doRecharge.html");
			$("#J_reCharge").attr("onsubmit","return reCharge();");
			var newCashBtn1 = '<input type="submit" name="name" class="cashBtn" value="确认提交"/>';
			$(".paymentStatus").prev().remove();
			$(".paymentStatus").before(newCashBtn1);
		}
	})


	function Inputlimit(obj,type){
		if (type==1) {
			obj.value=obj.value.replace(/\D/g,'')
		}else{
			obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
			obj.value = obj.value.replace(/^\./g,""); //验证第一个字符是数字而不是
			obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的
			obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
			obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
		}
	}
	$('input[name="money"]').keyup(function(){

		Inputlimit(this,0)
	})
    	
    	//金额校验
    	var flag;//充值标记
    	function validateMoney(){
    		var moneyVal = $('input[name="money"]').val();
    		if(moneyVal == "")
    		{
    			$('input[name="money"]').focus();
    			$(".money-error").html("请输入充值金额");
    			flag = false;
    		}
    		else if(parseFloat(moneyVal) < 100)
    		{
    			$('input[name="money"]').focus();
    			$(".money-error").html("充值金额不能低于100元");
    			flag = false;
    		}
    		else
    		{
    			$(".money-error").html("");
    			flag = true;
    		}
    	}
    	$('input[name="money"]').on("keyup",function(){
    		validateMoney();
    	});
    	$("#code").keyup(function(){
    		validCode();
    	});
    	
    	function validCode(){
    		if($("#code").val()==""){
	    		$(".vaild-error").html("验证码不能为空");
	    		return false;
	    	}else if($("#code").val().length!=6){
    			$(".vaild-error").html("验证码长度为6位");
    			return false;
    		}else{
    			$(".vaild-error").html("");
    			return true;
    		}
    	}
    	//提交验证
    	function reCharge(){
    	
    		validateMoney();
    		
    		
    		if(flag)
    		{
					var val=$('.paymentStatus').val();
					//线下转账
					if(val==2)
					{
						   $("form").ajaxSubmit({
							   dataType:'json',
							   success:function(data){

								   //单点登录检测
								   if(Auth.checkLogin(arguments[0]) == false) {return ;}

								   if(data.result==true){
		        					window.layer.open({
									    type: 1,
									    closeBtn: 1,
						                title: "&nbsp;",
									    area: ['384px', '186px'],
									    border: [1, 1, '#cecfd0'],
									    time:3000,
									    content: '<div class="tipsWrap w384"><div class="tipsTxt"><i class="iconfont okIco">&#xe63d;</i><span>充值申请提交成功，请耐心等待审核！</span></div><div class="tipsBtnBar"><a href="javascript:;" class="okBtn">确定</a></div></div>',
									    cancel: function(index){
									    	window.location.reload();
									    },
									    end: function(){
									    	window.location.reload();
									    },
									    success: function(layero){
									    	var time =3; 
									    	function closeTime () {
												time--;
												$(".tipsMsg").html(time+"秒后窗口自动关闭");
		    								}
									    	setInterval(function(){
												closeTime();
											}, 1000);
									    }
									});
									$(".okBtn").click(function(){
										window.location.href = "/member/recharge/log.html";
									});
									$(".cancleBtn").click(function(){
										window.location.reload();
									});
							        	}
							        	else
							        	{
						        			window.layer.open({
											    type: 1,
											    closeBtn: 1,
								                title: "&nbsp;",
											    area: ['384px', '186px'],
											    border: [1, 1, '#cecfd0'],
											    content: '<div class="tipsWrap w384"><div class="tipsTxt"><i class="iconfont errIco">&#xe63e;</i><span>'+data.msg+'</span></div><div class="tipsBtnBar"><a href="javascript:;" class="okBtn failBtn">确定</a></div></div>',
											    close: function(index){
											    	layer.closeAll();
											    	window.location.reload();
											    	$(".valicode_img").each(function(){
													$(this).attr("src",'/validimg.html?t=' + Math.random());
									        			})
											    }
											});
											$(".failBtn").click(function(){
												layer.closeAll();
												$(".valicode_img").each(function(){
													$(this).attr("src",'/validimg.html?t=' + Math.random());
								        				})
											});
							        	}
							}
						   })
					}

			return true;
    		}
    		else
    		{
    			return false;
    		}
    	}

    	exports.reCharge = reCharge;

	function show_recharge(){
		$(".J_valicode_img").each(function(){
			$(this).attr("src",'/validimg.html?t=' + Math.random());
		})
		jQuery( "#modal_dialog" ).dialog({ autoOpen: false , modal: true ,height: 160,width:360 });
		jQuery( "#modal_dialog" ).dialog( "open" );
	}

	//展开更多银行
	//if($('.bank_list>li').length>5)
	//{
	//    var $bank = $('.bank_list li:gt(3):not(:last)');
	//    $bank.hide();
	//	$('.bank_list').append('<li class="otherBank show">请选择其他银行<i class="iconfont">&#xe650;</i></li>');
	//	$('.otherBank').on('click',function(){
	//		$bank.show();
	//		$(this).hide();
	//	})
    //}else{
    	//$('.otherBank').remove();
    //}
    
    //点击隐藏表格
    $('.btn_hide').on('click',function(){
    	$('.user_cash_list').hide();
    	$('.recharge_list').css("border","none");
    	$('.user_cash_form .cashBtn').css("margin-top","0px")
    })
    $('.active2').on('click',function(){
    	$('.user_cash_list').show();
    	$('.recharge_list').css("border-bottom","solid 1px #e6e6e6");
        $('.user_cash_form .cashBtn').css("margin-top","30px")
    })

	//表格单双行变色
	$('.user_cash_list tr:gt(0):even').css("background","#e1e4e9");
	$('.user_cash_list tr:gt(0):odd').css("background","#fafbfc");
	$('.user_cash_list tr td:last-child').addClass('font_size');

	//选中单选按钮
	$('._radioCheck li').on("click",function(){
		if($(this).find('input:radio[type=radio]'))
		{
			$(this).find('input:radio[type=radio]').prop("checked",true);
		}
	})

	$('.bank_list li').on("click",function(){
		if(!$(this).hasClass("otherBank"))
		{
			$(this).addClass("active").siblings().removeClass("active");
		}
	});
	//默认选中第一个
	$(".bank_list li:first").addClass("active");
	$(".bank_list li:first input:radio[type=radio]").prop("checked",true);


	//计算充值后可用余额
	$('input[name="money"]').keyup(function(){
		var rechargeMoney = $(this).val();
		if(rechargeMoney == "")
		{
			rechargeMoney	 = 0;
		}
		else
		{
			rechargeMoney = parseFloat(rechargeMoney);
		}
		var remainMoney = parseFloat($("#remainMoney").attr("value"));

		var rechargeSuccessMoney = rechargeMoney +  remainMoney;
		if(rechargeSuccessMoney == 0)
		{
			rechargeSuccessMoney = rechargeSuccessMoney + ".00";
		}
		else
		{
			var n = 2; 
			rechargeSuccessMoney = parseFloat((rechargeSuccessMoney + "").replace(/[^\d\.-]/g, "")).toFixed(n) + ""; 
			var l = rechargeSuccessMoney.split(".")[0].split("").reverse(), r = rechargeSuccessMoney.split(".")[1]; 
			var t = ""; 
			for (var i = 0; i < l.length; i++) { 
			t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : ""); 
			} 
			rechargeSuccessMoney = t.split("").reverse().join("") + "." + r;
		}
		$("#rechargeSuccessMoney").html(rechargeSuccessMoney);
	})
	
});
